#include "DIALOG.h"
#include "MC_include.h"
#include "Maintask.h"

#define ID_FRAMEWIN_1     (GUI_ID_USER + 0x40)
#define ID_EMULTIEDIT_0     (GUI_ID_USER + 0x201)
#define ID_EBUTTON_0     (GUI_ID_USER + 0x202)
#define ID_EBUTTON_1     (GUI_ID_USER + 0x203)
#define ID_EBUTTON_2     (GUI_ID_USER + 0x204)
#define ID_EBUTTON_3     (GUI_ID_USER + 0x205)
#define ID_EBUTTON_4     (GUI_ID_USER + 0x206)
#define ID_EBUTTON_5     (GUI_ID_USER + 0x207)
#define ID_EBUTTON_6     (GUI_ID_USER + 0x208)
#define ID_EBUTTON_7     (GUI_ID_USER + 0x209)
#define ID_EBUTTON_8     (GUI_ID_USER + 0x20A)
#define ID_EBUTTON_9     (GUI_ID_USER + 0x20B)
#define ID_EBUTTON_10     (GUI_ID_USER + 0x20C)
#define ID_EBUTTON_11     (GUI_ID_USER + 0x20D)
#define ID_EBUTTON_12     (GUI_ID_USER + 0x20E)
#define ID_EBUTTON_13     (GUI_ID_USER + 0x20F)
#define ID_EBUTTON_14     (GUI_ID_USER + 0x210)
#define ID_EBUTTON_15     (GUI_ID_USER + 0x211)
#define ID_EBUTTON_16     (GUI_ID_USER + 0x212)
#define ID_EBUTTON_17     (GUI_ID_USER + 0x213)
#define ID_EBUTTON_18     (GUI_ID_USER + 0x214)
#define ID_EBUTTON_19     (GUI_ID_USER + 0x215)
#define ID_EBUTTON_20     (GUI_ID_USER + 0x216)
#define ID_EBUTTON_21     (GUI_ID_USER + 0x217)
#define ID_EBUTTON_22     (GUI_ID_USER + 0x218)
#define ID_EBUTTON_23     (GUI_ID_USER + 0x219)
#define ID_EBUTTON_24     (GUI_ID_USER + 0x21A)
#define ID_EBUTTON_25     (GUI_ID_USER + 0x21B)
#define ID_EBUTTON_26     (GUI_ID_USER + 0x21C)
#define ID_EBUTTON_27     (GUI_ID_USER + 0x21D)
#define ID_EBUTTON_28     (GUI_ID_USER + 0x21E)
#define ID_EBUTTON_29     (GUI_ID_USER + 0x21F)
#define ID_ETEXT_0        (GUI_ID_USER + 0x220)

#define ID_NAMEFRAMEWIN_0     (GUI_ID_USER + 0x221)
#define ID_NAMEEDIT_0         (GUI_ID_USER + 0x222)
#define ID_NAMEBUTTON_0       (GUI_ID_USER + 0x223)

extern uint8_t editWin_flag;
uint8_t E_Pressed = 0;
uint8_t save_flag = 0;
uint8_t Is_new_file_flag = 1;
uint8_t open_edit_flag = 0;

WM_HWIN edithWin;
char file_name_buff[8] = {0};
char file_path_buff[15] = {0};

static void _cbNameDialog(WM_MESSAGE * pMsg);

static const GUI_WIDGET_CREATE_INFO _aDialogEitorwinCreate[] =
{
    { FRAMEWIN_CreateIndirect, "Framewin", ID_FRAMEWIN_1, 0, 0, 800, 480, 0, 0x64, 0 },
    { MULTIEDIT_CreateIndirect, "Multiedit", ID_EMULTIEDIT_0, 22, 21, 412, 364, 0, 0x0, 0 },
    { BUTTON_CreateIndirect, "OPEN", ID_EBUTTON_0, 455, 20, 48, 48, 0, 0x0, 0 },
    { BUTTON_CreateIndirect, "SAVE", ID_EBUTTON_1, 515, 20, 48, 48, 0, 0x0, 0 },
    { BUTTON_CreateIndirect, "New", ID_EBUTTON_2, 575, 20, 48, 48, 0, 0x0, 0 },
    { BUTTON_CreateIndirect, " ", ID_EBUTTON_3, 635, 20, 48, 48, 0, 0x0, 0 },
    { BUTTON_CreateIndirect, " ", ID_EBUTTON_4, 695, 20, 48, 48, 0, 0x0, 0 },
    { BUTTON_CreateIndirect, "X", ID_EBUTTON_5, 455, 85, 48, 48, 0, 0x0, 0 },
    { BUTTON_CreateIndirect, "Y", ID_EBUTTON_6, 515, 85, 48, 48, 0, 0x0, 0 },
    { BUTTON_CreateIndirect, "Z", ID_EBUTTON_7, 575, 85, 48, 48, 0, 0x0, 0 },
    { BUTTON_CreateIndirect, "I", ID_EBUTTON_8, 635, 85, 48, 48, 0, 0x0, 0 },
    { BUTTON_CreateIndirect, "J", ID_EBUTTON_9, 695, 85, 48, 48, 0, 0x0, 0 },
    { BUTTON_CreateIndirect, "G", ID_EBUTTON_10, 455, 150, 48, 48, 0, 0x0, 0 },
    { BUTTON_CreateIndirect, "1", ID_EBUTTON_11, 515, 150, 48, 48, 0, 0x0, 0 },
    { BUTTON_CreateIndirect, "2", ID_EBUTTON_12, 575, 150, 48, 48, 0, 0x0, 0 },
    { BUTTON_CreateIndirect, "3", ID_EBUTTON_13, 635, 150, 48, 48, 0, 0x0, 0 },
    { BUTTON_CreateIndirect, "T", ID_EBUTTON_14, 695, 150, 48, 48, 0, 0x0, 0 },
    { BUTTON_CreateIndirect, "M", ID_EBUTTON_15, 455, 210, 48, 48, 0, 0x0, 0 },
    { BUTTON_CreateIndirect, "4", ID_EBUTTON_16, 515, 210, 48, 48, 0, 0x0, 0 },
    { BUTTON_CreateIndirect, "5", ID_EBUTTON_17, 575, 210, 48, 48, 0, 0x0, 0 },
    { BUTTON_CreateIndirect, "6", ID_EBUTTON_18, 635, 210, 48, 48, 0, 0x0, 0 },
    { BUTTON_CreateIndirect, "BAK", ID_EBUTTON_19, 695, 210, 48, 48, 0, 0x0, 0 },
    { BUTTON_CreateIndirect, "S", ID_EBUTTON_20, 455, 270, 48, 48, 0, 0x0, 0 },
    { BUTTON_CreateIndirect, "7", ID_EBUTTON_21, 515, 270, 48, 48, 0, 0x0, 0 },
    { BUTTON_CreateIndirect, "8", ID_EBUTTON_22, 575, 270, 48, 48, 0, 0x0, 0 },
    { BUTTON_CreateIndirect, "9", ID_EBUTTON_23, 635, 270, 48, 48, 0, 0x0, 0 },
    { BUTTON_CreateIndirect, "Cancel", ID_EBUTTON_24, 695, 270, 48, 48, 0, 0x0, 0 },
    { BUTTON_CreateIndirect, "R", ID_EBUTTON_25, 455, 335, 48, 48, 0, 0x0, 0 },
    { BUTTON_CreateIndirect, "-", ID_EBUTTON_26, 515, 335, 48, 48, 0, 0x0, 0 },
    { BUTTON_CreateIndirect, "0", ID_EBUTTON_27, 575, 335, 48, 48, 0, 0x0, 0 },
    { BUTTON_CreateIndirect, ".", ID_EBUTTON_28, 635, 335, 48, 48, 0, 0x0, 0 },
    { BUTTON_CreateIndirect, "Enter", ID_EBUTTON_29, 695, 335, 48, 48, 0, 0x0, 0 },
    { TEXT_CreateIndirect, "Text", ID_ETEXT_0, 25, 395, 405, 29, 0, 0x64, 0 },
};

//重命名对话框
static const GUI_WIDGET_CREATE_INFO _NameaDialogCreate[] =
{
    { FRAMEWIN_CreateIndirect, "Framewin", ID_NAMEFRAMEWIN_0, 25, 200, 278, 89, 0, 0x64, 0 },
    { EDIT_CreateIndirect, "Edit", ID_NAMEEDIT_0, 10, 15, 135, 25, 0, 0x5, 0 },
    { BUTTON_CreateIndirect, "Button", ID_NAMEBUTTON_0, 168, 15, 80, 25, 0, 0x0, 0 },
};

static void _cbEitorwinDialog(WM_MESSAGE * pMsg)
{
    WM_HWIN hItem;
    E_Pressed = 0;
    int     NCode;
    int     Id;
    // USER START (Optionally insert additional variables)
    // USER END

    switch (pMsg->MsgId)
    {
        case WM_INIT_DIALOG:
            //
            // Initialization of 'Framewin'
            //
            hItem = pMsg->hWin;
            FRAMEWIN_SetSkinClassic(hItem);
            // FRAMEWIN_SetActive(hItem, 1);
            FRAMEWIN_SetTitleHeight(hItem, 32);
            FRAMEWIN_SetFont(hItem, &GUI_Fontfont20B);
            FRAMEWIN_SetTextAlign(hItem, GUI_TA_LEFT | GUI_TA_VCENTER);
            FRAMEWIN_SetText(hItem, "编辑");
            FRAMEWIN_AddCloseButton(hItem, FRAMEWIN_BUTTON_RIGHT, 0);
            FRAMEWIN_SetClientColor(hItem, GUI_MAKE_COLOR(0x00E7E7E7));
            WM_MakeModal(hItem);

            hItem = WM_GetDialogItem(pMsg->hWin, ID_EMULTIEDIT_0);
            MULTIEDIT_SetText(hItem, "");
            MULTIEDIT_SetFont(hItem, GUI_FONT_24B_1);
            MULTIEDIT_SetAutoScrollV(hItem, 1);

            hItem = WM_GetDialogItem(pMsg->hWin, ID_ETEXT_0);
            TEXT_SetTextAlign(hItem, GUI_TA_LEFT | GUI_TA_VCENTER);
            TEXT_SetFont(hItem, GUI_FONT_20_1);
            TEXT_SetText(hItem, "New file");


            //按键初始化
            for(int i = ID_EBUTTON_5; i <= ID_EBUTTON_29; i ++)
            {
                hItem = WM_GetDialogItem(pMsg->hWin, i);
                BUTTON_SetFocussable(hItem, 0);  //按键失去聚焦
                BUTTON_SetFont(hItem, GUI_FONT_16B_1);
            }

            break;

        case WM_DELETE:
            editWin_flag = 0;
            save_flag = 0;
            Is_new_file_flag = 1;
            break;

        case WM_NOTIFY_PARENT:
            Id    = WM_GetId(pMsg->hWinSrc);
            NCode = pMsg->Data.v;

            if(Id >= ID_EBUTTON_5 && Id <= ID_EBUTTON_29)
            {
                switch(NCode)
                {
                    case WM_NOTIFICATION_CLICKED:
                        E_Pressed = 1;

                    case WM_NOTIFICATION_RELEASED:
                    {
                        int Key;

                        if((Id != ID_EBUTTON_19) && (Id < ID_EBUTTON_29))
                        {
                            char acBuffer[20];
                            BUTTON_GetText(pMsg->hWinSrc, acBuffer, sizeof(acBuffer)); /* Get the text of the button */
                            Key = acBuffer[0];
                        }
                        else if(Id == ID_EBUTTON_19)
                            Key = GUI_KEY_BACKSPACE;
                        else if(Id == ID_EBUTTON_29)
                            Key = GUI_KEY_ENTER;


                        GUI_SendKeyMsg(Key, E_Pressed); /*Send a key message to the focussed window */
                    }
                    break;

                    default:
                        WM_DefaultProc(pMsg);
                }
            }

            switch(Id)
            {
                case ID_EMULTIEDIT_0: // Notifications sent by 'Multiedit'
                    switch(NCode)
                    {
                        case WM_NOTIFICATION_CLICKED:
                            // USER START (Optionally insert code for reacting on notification message)
                            // USER END
                            break;

                        case WM_NOTIFICATION_RELEASED:
                            // USER START (Optionally insert code for reacting on notification message)
                            // USER END
                            break;

                        case WM_NOTIFICATION_VALUE_CHANGED:
                            // USER START (Optionally insert code for reacting on notification message)
                            // USER END
                            break;
                            // USER START (Optionally insert additional code for further notification handling)
                            // USER END
                    }

                    break;

                case ID_EBUTTON_1: //保存
                    switch(NCode)
                    {
                        case WM_NOTIFICATION_CLICKED:
                            if(Is_new_file_flag == 1)
                            {
                                GUI_CreateDialogBox(_NameaDialogCreate, GUI_COUNTOF(_NameaDialogCreate), _cbNameDialog, edithWin, 0, 0);
                            }
                            else if(Is_new_file_flag == 0)
                            {
                                save_flag = 1;
                            }

                            break;

                        case WM_NOTIFICATION_RELEASED:
                            break;
                    }

                    break;

                case ID_EBUTTON_0:
                    switch(NCode)
                    {
						case WM_NOTIFICATION_CLICKED:
							
							break;
                        case WM_NOTIFICATION_RELEASED:
							CreateFilewin(edithWin);
                            break;						
                    }
                    break;
                case ID_EBUTTON_2: //NEW
                    switch(NCode)
                    {
						case WM_NOTIFICATION_CLICKED:
							
							break;
                        case WM_NOTIFICATION_RELEASED:
							Is_new_file_flag = 1;
							hItem = WM_GetDialogItem(pMsg->hWin, ID_EMULTIEDIT_0);
							MULTIEDIT_SetText(hItem,"");
                            break;						
                    }
                    break;
            }

            break;

        // USER START (Optionally insert additional message handling)
        // USER END
        default:
            WM_DefaultProc(pMsg);
            break;
    }
}

static void _cbNameDialog(WM_MESSAGE * pMsg)
{
    WM_HWIN hItem;
    int     NCode;
    int     Id;
    // USER START (Optionally insert additional variables)
    // USER END

    switch (pMsg->MsgId)
    {
        case WM_INIT_DIALOG:
            //
            // Initialization of 'Framewin'
            //
            hItem = pMsg->hWin;
            FRAMEWIN_SetSkinClassic(hItem);
            FRAMEWIN_SetTitleHeight(hItem, 24);
            FRAMEWIN_SetText(hItem, "SAVE");
            FRAMEWIN_SetFont(hItem, GUI_FONT_16B_1);
            FRAMEWIN_AddCloseButton(hItem, FRAMEWIN_BUTTON_RIGHT, 0);
            FRAMEWIN_SetClientColor(hItem, GUI_MAKE_COLOR(0x00E7E7E7));
            FRAMEWIN_SetActive(hItem, 1);

            //
            // Initialization of 'Edit'
            //
            hItem = WM_GetDialogItem(pMsg->hWin, ID_NAMEEDIT_0);
            EDIT_SetText(hItem, "");
            EDIT_SetFont(hItem, GUI_FONT_24_1);
            //
            // Initialization of 'Button'
            //
            hItem = WM_GetDialogItem(pMsg->hWin, ID_NAMEBUTTON_0);
            BUTTON_SetText(hItem, "OK");
            BUTTON_SetFont(hItem, GUI_FONT_16B_1);
            // USER START (Optionally insert additional code for further widget initialization)
            Is_new_file_flag = 3;
            // USER END
            break;

        case WM_DELETE:
            Is_new_file_flag = 1;
            break;

        case WM_NOTIFY_PARENT:
            Id    = WM_GetId(pMsg->hWinSrc);
            NCode = pMsg->Data.v;

            switch(Id)
            {
                case ID_NAMEEDIT_0: // Notifications sent by 'Edit'
                    switch(NCode)
                    {
                        case WM_NOTIFICATION_CLICKED:
                            // USER START (Optionally insert code for reacting on notification message)
                            // USER END
                            break;

                        case WM_NOTIFICATION_RELEASED:
                            // USER START (Optionally insert code for reacting on notification message)
                            // USER END
                            break;

                        case WM_NOTIFICATION_VALUE_CHANGED:
                            // USER START (Optionally insert code for reacting on notification message)
                            // USER END
                            break;
                            // USER START (Optionally insert additional code for further notification handling)
                            // USER END
                    }

                    break;

                case ID_NAMEBUTTON_0: // Notifications sent by 'Button'
                    switch(NCode)
                    {
                        case WM_NOTIFICATION_CLICKED:
                            // USER START (Optionally insert code for reacting on notification message)
                            // USER END
                            break;

                        case WM_NOTIFICATION_RELEASED:
                            hItem = WM_GetDialogItem(pMsg->hWin, ID_NAMEEDIT_0);
                            EDIT_GetText(hItem, file_name_buff, 5);
                            save_flag = 1;
                            GUI_EndDialog(pMsg->hWin, 0);
                            break;
                            // USER START (Optionally insert additional code for further notification handling)
                            // USER END
                    }

                    break;
                    // USER START (Optionally insert additional code for further Ids)
                    // USER END
            }

            break;

        // USER START (Optionally insert additional message handling)
        // USER END
        default:
            WM_DefaultProc(pMsg);
            break;
    }
}

FIL text_file;
FRESULT text_res;
UINT text_fnum;
extern char celfile_text_buffer[11];

WM_HWIN CreateEitorwin(void);
WM_HWIN CreateEitorwin(void)
{
    WM_HWIN hItem;
    uint32_t textSize;
    edithWin = GUI_CreateDialogBox(_aDialogEitorwinCreate, GUI_COUNTOF(_aDialogEitorwinCreate), _cbEitorwinDialog, HDTWIN, 0, 0);

    while(editWin_flag)
    {
        if(save_flag)
        {
            sprintf(file_path_buff, "1:\\user\\%s.gc", file_name_buff);
            text_res = f_open(&text_file, file_path_buff, FA_CREATE_ALWAYS | FA_WRITE );
            hItem = WM_GetDialogItem(edithWin, ID_EMULTIEDIT_0);
            textSize = MULTIEDIT_GetTextSize(hItem);
            char * textBuffer = (char *)malloc(sizeof(char) * textSize);
			//memset(textBuffer,0,);
			printf("textBuffer size is %d\r\n",sizeof(*textBuffer)/sizeof(char));
            if(!textBuffer)
                printf("malloc fail\r\n");

            MULTIEDIT_GetText(hItem, textBuffer, textSize);
            text_res = f_write(&text_file, textBuffer, textSize, &text_fnum);

            if(text_res == FR_OK)
            {
                printf(">文件写入成功\r\n");
				printf("写入的文件数据是\r\n%s",textBuffer);
            }
            else
                printf(">文件写入失败(%d)\r\n", text_res);

            f_close(&text_file);
            free(textBuffer);
            save_flag = 0;
            Is_new_file_flag = 0;
        }

        if(Is_new_file_flag == 0)
        {
            hItem = WM_GetDialogItem(edithWin, ID_ETEXT_0);
            TEXT_SetText(hItem, file_name_buff);
        }
        else if(Is_new_file_flag == 1)
        {
            hItem = WM_GetDialogItem(edithWin, ID_ETEXT_0);
            TEXT_SetText(hItem, "New file");
        }

		if(open_edit_flag)
		{
			char read_path[20] = {0};
			sprintf(read_path,"1:/user/%s",celfile_text_buffer);
			text_res = f_open(&text_file,read_path,FA_OPEN_EXISTING|FA_WRITE|FA_READ);
			if(text_res == FR_OK)
			{
				char read_buffer[1024] ={0};
				text_res = f_lseek(&text_file,0);
				text_res = f_read(&text_file,read_buffer,f_size(&text_file),&text_fnum);
				if(text_res == FR_OK)
				{
					hItem = WM_GetDialogItem(edithWin, ID_EMULTIEDIT_0);
					MULTIEDIT_SetText(hItem,"");	
					MULTIEDIT_SetText(hItem,read_buffer);		
					printf("%s",read_buffer);
				}
				//free(read_buffer);
				Is_new_file_flag = 0;
				sprintf(file_name_buff,"%s", celfile_text_buffer);
			}
			else
			{
				printf("file not existing\r\n");
			}
			f_close(&text_file);
			open_edit_flag = 0;
			
		}
		

        GUI_Delay(30);
    }

    return edithWin;
}

// USER START (Optionally insert additional public code)
// USER END

/*************************** End of file ****************************/
